package com.ruoyi.framework.xa;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.ruoyi.framework.web.domain.server.Sys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.sql.DataSource;
import javax.transaction.UserTransaction;
import java.util.Properties;

@Configuration
public class XADataSourceConfig {

    @Autowired
    private Environment env;

    @Bean(name = "masterDataSource")//第一个数据源
    @Primary
    public DataSource masterDataSource(Environment env) {
        String prefix = "spring.datasource.master.";
        String dataSourceName = "master";
        return getDataSource(env, prefix, dataSourceName);
    }

    @Bean(name = "slaveDataSource")//第二个数据源
    public DataSource slaveDataSource(Environment env) {
        String prefix = "spring.datasource.slave.";
        String dataSourceName = "slave";
        return getDataSource(env, prefix, dataSourceName);
    }

    protected DataSource getDataSource(Environment env, String prefix, String dataSourceName) {
        Properties prop = build(env, prefix);
        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        // 添加连接池限制
        ds.setMaxPoolSize(50);
        ds.setMinPoolSize(5);
        ds.setBorrowConnectionTimeout(60);
        ds.setUniqueResourceName(dataSourceName);
        ds.setXaProperties(prop);
        return ds;
    }

    @Bean("mJdbcTemplate")//数据源实例
    public JdbcTemplate jamesJdbcTemplate(@Qualifier("masterDataSource") DataSource ds) {
        return new JdbcTemplate(ds);
    }

    @Bean("sJdbcTemplate")//数据源实例
    public JdbcTemplate peterJdbcTemplate(@Qualifier("slaveDataSource") DataSource ds) {
        return new JdbcTemplate(ds);
    }

    @Bean//创建事务管理器
    public JtaTransactionManager regTransactionManager() {
        UserTransaction userTransaction = new UserTransactionImp();
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        return new JtaTransactionManager(userTransaction, userTransactionManager);
    }

    private Properties build(Environment env, String prefix) {
        Properties prop = new Properties();
        prop.put("url", env.getProperty(prefix + "url"));
        prop.put("username", env.getProperty(prefix + "username"));
        prop.put("password", env.getProperty(prefix + "password"));
        System.setProperty("druid.driverClassName", env.getProperty(prefix + "driverClassName"));
        return prop;
    }
}